package com.example.activiti7.controller;

import com.example.activiti7.domain.model.req.QueryProcessInstanceListReq;
import com.example.activiti7.domain.model.resp.Page;
import com.example.activiti7.domain.model.resp.Response;
import com.example.activiti7.domain.model.vo.HistoricProcessInstanceVO;
import com.example.activiti7.domain.model.vo.ProcessInstanceVO;
import com.example.activiti7.domain.service.ProcessInstanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/overflow/instance")
@Tag(name = "流程实例服务能力", description = "流程实例接口服务管理")
public class ProcessInstanceController {
    private final ProcessInstanceService processInstanceService;

    public ProcessInstanceController(ProcessInstanceService processInstanceService) {
        this.processInstanceService = processInstanceService;
    }

    @GetMapping(value = "/preview/{processInstanceId}.svg")
    @Parameter(name = "processInstanceId", description = "流程实例Id", required = true)
    @Operation(summary = "根据流程实例ID预览流程图")
    public void previewDiagram(HttpServletResponse response, @PathVariable("processInstanceId") String processInstanceId) throws IOException {
        processInstanceService.previewDiagram(response, processInstanceId);
    }

    @DeleteMapping("/{processInstanceId}")
    @Parameter(name = "processInstanceId", description = "流程实例Id", required = true)
    @Operation(summary = "根据流程实例ID删除流程实例")
    public Response<Boolean> deleteByProcessInstanceID(@PathVariable("processInstanceId") String processInstanceId) {
        return processInstanceService.deleteByProcessInstanceID(processInstanceId);
    }

    @GetMapping("/{processDefinitionId}")
    @Parameter(name = "processDefinitionId", description = "流程实例Id", required = true)
    @Operation(summary = "根据流程定义ID获取日志列表")
    public Response<List<HistoricProcessInstanceVO>> queryHisByProcessDefinitionId(@PathVariable("processDefinitionId") String processDefinitionId) {
        return processInstanceService.queryHisByProcessDefinitionId(processDefinitionId);
    }
    
    @PostMapping("/list")
    @Operation(summary = "根据条件查询流程实例列表")
    public Response<Page<ProcessInstanceVO, QueryProcessInstanceListReq>> queryProcessInstances(@RequestBody QueryProcessInstanceListReq req) throws Exception {
        return processInstanceService.queryProcessInstances(req);
    }

}
